// pages/person/income/index.js
import {throttle} from "../../../../utils/util"
const $api = require('../../../../request/api').API;
import {formatDate} from "../../../../utils/util"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNo:1,
        pageSize:10,
        totalPage:"",
        listRecord:[],
        prevDisabled:false,
        nextDiasbled:false,

        showTimeSelect:false,
        currentDate: new Date().getTime(),
        maxDate:new Date().getTime(),
        formatCurrentDate:"选择时间",
        formatter(type, value) {
          if (type === 'year') {
            return `${value}年`;
          }
          if (type === 'month') {
            return `${value}月`;
          }
          return value;
        },
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            currentDate:new Date().getTime(),
            formatCurrentDate:formatDate(new Date()).substr(0,7)
        })
        this.getData()
    },
    getData(){
        // 获取收入明细列表
        let params = {
            pageNo:this.data.pageNo,
            pageSize:this.data.pageSize,
            beginDate:this.data.formatCurrentDate+"-01"
        }
        $api.cashOutRecord(params).then(res=>{
            res.data.data.list.forEach(item=>{
                let temp = this.formatCashOutState(item.cashOutState)
                item.cashOutState = temp.content,
                item.color = temp.color
            })
            this.setData({
                listRecord:res.data.data.list,
                totalPage:res.data.data.totalPage
            })
        })
    },
    selelctTime(){
        this.setData({
            showTimeSelect:!this.data.showTimeSelect,
        })
        
    },
    onClose() {
        this.setData({ showTimeSelect: false });
    },
    // 确认时间
    confirmTime(e){
        let date = formatDate(new Date(e.detail)).substr(0,7)
        this.setData({
            currentDate:e.detail,
            formatCurrentDate:date,
            pageNo:1,
            showTimeSelect:false
        })

        this.getData()
        
    },
    // 取消时间
    cancelTime(e){
        this.setData({
            showTimeSelect:false
        })
    },
    /* 上一页 */
    prevPage:throttle(function(){
        if(this.data.pageNo<=1){
            this.setData({
                pageNo:1
            })
            return
        }
        this.setData({
            pageNo:--this.data.pageNo
        })
        this.getData()
    },300),
    /* 下一页 */
    nextPage:throttle(function(){
        if(this.data.pageNo>=this.data.totalPage){
            this.setData({
                pageNo:this.data.totalPage
            })
            return
        }
        this.setData({
            pageNo:++this.data.pageNo
        })
        this.getData()
    },500),
     // 转账状态
    formatCashOutState(type){
        switch(type){
            case 1:
            return {content:"转账中",color:'#409EFF'}
            case 2:
            return {content:"微信SYSTEM_ERROR",color:'#E6A23C'}
            case 19:
            return {content:"转账成功",color:'green'}
            case 20:
            return {content:"转账失败",color:'red'}
            default:
            return
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})